import request from './index.js';
import { getRefreshToken } from './token.js';

let promise;

export async function refreshtoken() {
  if (promise) {
    return promise;
  }
  // eslint-disable-next-line no-async-promise-executor
  promise = new Promise(async (resolve) => {
    const resp = await request.get('/refresh_token', {
      headers: {
        Authorization: `Bearer ${getRefreshToken()}`,
      },
      __isRefreshToke: true,
    });

    resolve(resp.code === 0);
  });

  promise.finally(() => {
    promise = null;
  });

  return promise;
}

export async function isRefreshRequest(config) {
  return !!config.__isRefreshToke;
}
